package Demo1;

import java.util.Arrays;

class Student implements Comparable<Student>{
    String name;
    int age;
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age-o.age;
    }
}
public class Test {
public static void mySort(Comparable comparable[]){
    for (int i = 0; i < comparable.length-1; i++) {
        for (int j = 0; j < comparable.length-1-i; j++) {
            if(comparable[j].compareTo(comparable[j+1])>0){
                Comparable temp=comparable[j];
                comparable[j]=comparable[j+1];
                comparable[j+1]=temp;
            }
        }
    }
}
    public static void main1(String[] args) {
        Student[] student=new Student[3];
        student[0]=new Student("小明",8);
        student[1]=new Student("小红",8);
        student[2]= new Student("小张",6);
        System.out.println("排序前：");
        for (Student s:student) {
            System.out.println(s);
        }
        mySort(student);
        System.out.println("排序后：");
        for (Student s:student) {
            System.out.println(s);
        }
    }



    public static void main(String[] args) {
        Student[] student=new Student[3];
        student[0]=new Student("xiaozhang",8);
        student[1]=new Student("xiaohong",8);
        student[2]= new Student("xiaoming",6);
        NameComparator namecomparator=new NameComparator();
        int ret=namecomparator.compare(student[1],student[2]);
        System.out.println(ret);
        System.out.println("排序前：");
        for (Student s:student) {
            System.out.println(s);
        }
//        mySort(student);
        Arrays.sort(student,namecomparator);
        System.out.println("排序后：");
        for (Student s:student) {
            System.out.println(s);
        }
    }
}
